package com.cqhilink.iot.jobstandardmanagement.services.innerservice.impl.datereport;

import com.alibaba.fastjson.JSONObject;
import com.cqhilink.api.common.utils.date.DateStyle;
import com.cqhilink.api.common.utils.date.DateUtil;
import com.cqhilink.api.common.utils.enums.RespCodeEnum;
import com.cqhilink.iot.jobstandardmanagement.facade.utils.BackMessage;
import com.cqhilink.iot.jobstandardmanagement.dao.mapper.RepItemMapper;
import com.cqhilink.iot.jobstandardmanagement.facade.model.Employee;
import com.cqhilink.iot.jobstandardmanagement.facade.model.RepItem;
import com.cqhilink.iot.jobstandardmanagement.facade.service.datareport.JsmRepItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by huangxueqian on 2017/2/17/017.
 * 报表数据项管理服务实现类
 */

public class JsmRepItemServiceImpl implements JsmRepItemService {


    @Autowired
    private RepItemMapper repItemMapper ;

    /**
     *
     * @param   repItem
     * @return  新增问题
     * @throws  Exception
     */
    @Override
    public Map<String, Object> addRepItem(RepItem repItem, Employee employee) throws Exception {
        Map<String, Object> resMap = new HashMap<>();
        if(repItem !=null ){
            repItem.setCreator(employee.getEmployeeId());
            repItem.setModifyer(employee.getEmployeeId());
            repItem.setCreatetime(DateUtil.DateToString(new Date(), DateStyle.YYYY_MM_DD_HH_MM_SS));
            repItem.setModifytime(DateUtil.DateToString(new Date(),DateStyle.YYYY_MM_DD_HH_MM_SS));
            repItemMapper.insertSelective(repItem);
            resMap.put("state", RespCodeEnum.RESP_CODE_ENUM_true.code());
            resMap.put("msg", BackMessage.ADD_OK);
        }else{
            repItem.setModifyer(employee.getEmployeeId());
            repItem.setModifytime(DateUtil.DateToString(new Date(),DateStyle.YYYY_MM_DD_HH_MM_SS));
            resMap.put("state", RespCodeEnum.RESP_CODE_ENUM_true.code());
            resMap.put("msg", BackMessage.MODIFY_OK);
        }
        return resMap ;
    }

    /**
     *
     * @param repItemIdList
     * @return 删除报表配置数据项
     * @throws Exception
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public Map<String, Object> delRepItem(List<String> repItemIdList) throws Exception {
        Map<String, Object> resMap = new HashMap<>();
        if(repItemIdList!=null){
            for(String itemId : repItemIdList){
                repItemMapper.deleteByPrimaryKey(itemId);
            }
            resMap.put("state",RespCodeEnum.RESP_CODE_ENUM_true);
            resMap.put("msg",BackMessage.DEL_OK);
        }else{
            resMap.put("state",RespCodeEnum.RESP_CODE_ENUM_false);
            resMap.put("msg",BackMessage.EXCEPTION_ISNULL);
        }
        return resMap;
    }

    /**
     * @param  repItem 查询问题
     * @throws Exception
     */
    @Override
    public RepItem queryRepItem(RepItem repItem) throws Exception {

        return null;
    }

    /**
     *
     * @param repItem
     * @return 查询
     * @throws Exception
     */
    @Override
    public JSONObject table(RepItem repItem) throws Exception {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("total",repItemMapper.queryRepItemCount(repItem));
            jsonObject.put("rows",repItemMapper.queryRepItemList(repItem));
        return jsonObject;
    }
}
